package com.lili.operate.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.common.result.Result;
import com.lili.model.operate.Distribution;
import com.lili.model.operate.DistributionCash;
import com.lili.model.operate.DistributionOrder;
import com.lili.operate.service.DistributionCashService;
import com.lili.operate.service.DistributionGoodsService;
import com.lili.operate.entity.distribution.DistributionGoods;
import com.lili.operate.service.DistributionOrderService;
import com.lili.operate.service.DistributionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/admin/distribution")
@Api(tags = "分销商品")
public class DistributionController {
    @Autowired
    DistributionGoodsService distributionGoodsService;

    @Autowired
    DistributionCashService distributionCashService;

    @Autowired
    DistributionOrderService distributionOrderService;

    @Autowired
    DistributionService distributionService;

    @ApiOperation("查询分销员")
    @PostMapping("/query/member")
    public Result queryDistributionMember(@RequestBody Map map){
        Page<Distribution> page = distributionService.queryDistribution(map);
        return page == null ? Result.fail() : Result.ok(page);
    }

    public Result updateDistributionStatus(@RequestBody Map map){
        long id = Long.parseLong(map.get("id").toString());
        String status = map.get("distributionStatus").toString();
        int rs = distributionService.updateStatus(id,status);
        return rs==1?Result.ok():Result.fail();
    }

    @ApiOperation("查询分销订单")
    @PostMapping("/query/order")
    public Result queryDistributionOrder(@RequestBody Map map){
        Page<DistributionOrder> page = distributionOrderService.queryDistributionOrder(map);
        return page == null ? Result.fail() : Result.ok(page);
    }


    @ApiOperation("查询分销体现")
    @PostMapping("/query/cash")
    public Result queryDistributionCash(@RequestBody Map map){
        Page<DistributionCash> page = distributionCashService.queryDistributionCash(map);
        return page == null ? Result.fail() : Result.ok(page);
    }

    @ApiOperation(value = "保存分销商品")
    @PostMapping("/save/goods")
    public Result saveDistributionGoods(@RequestBody  DistributionGoods distributionGoods){
        int rs = distributionGoodsService.saveDistributionGoods(distributionGoods);
        return rs==1?Result.ok():Result.fail();
    }

    @ApiOperation(value = "查询所有的分销商品")
    @PostMapping("/query/goods")
    public Result queryAllDistributionGoods(@RequestBody Map map){
        int current = 1;
        int pageSize = 2;
        if(map.get("current") != null){
            current = Integer.parseInt(map.get("current").toString());
        }
        if (map.get("pageSize") != null){
            pageSize = Integer.parseInt(map.get("pageSize").toString());
        }
        Page<DistributionGoods> page = distributionGoodsService.queryAllDistributionGoods(current,pageSize);
        return page == null ? Result.fail() : Result.ok(page);
    }
}
